//
//  StatusCellOriginalView.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/19.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class StatusCellOriginalView: UIView {
    //添加约束对象属性 做记录使用
    var bottomConstaint: Constraint?
    //添加模型属性
    var status:Status? {
        didSet {
            //绑定数据
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)
            nameLabel.text = status?.user?.name
            //TODO 以后完善
            timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
            sourceLabel.text = status?.sourceText
            mbrank_image.image = status?.user?.mbrank_image
            textLabel.text = status?.text
            //传递配图视图的数据源
            pictureView.imageURLs = status?.imageURLs
            //根据数据中是否有配图视图 然后动态调整约束关系  涉及到cell的复用 约束对象也会被复用
            //status?.imageURLs 永远都不为空 如果没有配图视图会给定一个空数组
            //where : 可以获取到前面快速赋值不为空的对象
            //如果imageURLs 不为空 并且 imageURLs.count 不为 0
            
            //解决约束冲突
            //1.先卸载老的约束对象  -> 使用属性 记录底部的约束对象
            self.bottomConstaint?.uninstall()
            //2.根据条件重新添加约束对象
            if let imageURLs = status?.imageURLs where imageURLs.count != 0 {
                //给原创微博添加底部约束
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                }
            }else{
                //给原创微博添加底部约束
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
                }
            }

        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        //添加子视图
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrank_image)
        addSubview(verified_type_image)
        addSubview(textLabel)
        addSubview(sepView)
        sepView.backgroundColor = UIColor.redColor()
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(10)
            
        }

        //自动布局
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            
            //约束大小
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_top)
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.top.equalTo(nameLabel.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(iconImageView.snp_bottom).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        verified_type_image.snp_makeConstraints { (make) -> Void in
            //在右下角位置
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        //图片切圆角
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true
        
        addSubview(pictureView)
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImageView.snp_left)
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            //TODO: 后续修改 先给定测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))

        }
        //给原创微博添加底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

    }
    
    private lazy var sepView: UIView = UIView()
    //懒加载控件 头像
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //用户名
    private lazy var nameLabel:UILabel = UILabel(text: "灏哥哥", fontSize: 16, textColor: UIColor.orangeColor())
    
    //时间
    private lazy var timeLabel:UILabel = UILabel(text: "22:22", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    //微博来源
    private lazy var sourceLabel:UILabel = UILabel(text: "隔壁老李", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    //微博等级
    private lazy var mbrank_image:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //用户认证类型
    private lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博内容
    private lazy var textLabel:UILabel = UILabel(text: "听说下雨天巧克力和辣条更配哦", fontSize: 16, textColor: UIColor.darkGrayColor(),alignment: NSTextAlignment.Left)
    
    //图片视图
    private lazy var pictureView:StatusCellPictureView = StatusCellPictureView()




}
